﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Task_When_ContinueFrom.cs" company="JDML Consulting Ltd.">
//   Copyright 2011 Photon Project - http://photon.codeplex.com
// </copyright>
// <summary>
//   Defines the Task_When_ContinueFrom type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Photon.Threading
{
    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Mocks;
    using Testing;
    using Testing.Threading;

    [TestClass]
   public class Task_When_ContinueFrom : AsynchronousTest
   {
      [TestMethod]
      public void If_Is_Already_Started_Should_Throw_InvalidOperationException()
      {
         var task = TestTaskFactories.Synchronous.From(() => MockDelegates.DoNothing);
         task.Start();
         
         // calling continue on started task should fail
         AssertException.Throws<InvalidOperationException>(() =>
            task.ContinueFrom(new MockContinuationSource()));
      }

      [TestMethod]
      public void If_Is_Already_Continuation_Should_Throw_InvalidOperationException()
      {
         var task = TestTaskFactories.Synchronous.From(() => MockDelegates.DoNothing);
         task.ContinueFrom(new MockContinuationSource());
         
         // calling continue again should fail
         AssertException.Throws<InvalidOperationException>(() => 
            task.ContinueFrom(new MockContinuationSource()));
      }

      //[TestMethod]
      //public void If_Callback_Is_Not_Null_Should_Invoke_Callback_On_Completion()
      //{
      //   var continuationSource = new MockContinuationSource();
         
      //   var task = TestFactories.Synchronous.From(() => MockDelegates.DoNothing);
      //   task.ContinueFrom(continuationSource, t => Log.Append("CallbackInvoked"));

      //   Assert.AreEqual(string.Empty, Log.Text);
      //   continuationSource.Completed();
      //   Assert.AreEqual("CallbackInvoked", Log.Text);
      //}
      
      //[TestMethod]
      //public void If_Continuation_Source_Has_Already_Completed_Should_Execute_Task()
      //{
      //   var continuationSource = new MockContinuationSource();
      //   continuationSource.Completed();

      //   var task = TestFactories.Synchronous.From(() => MockDelegates.DoNothing);
         
      //   Assert.AreEqual(string.Empty, Log.Text);
      //   task.ContinueFrom(continuationSource, t => Log.Append("CallbackInvoked"));
      //   Assert.AreEqual("CallbackInvoked", Log.Text);
      //}
   }
}
